<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card" body-style="height:100%">
      <el-row>
        <el-button type="primary" @click="addCategoriesDialog()">添加分类</el-button>
      </el-row>
      <el-dialog
        title="添加分类"
        :visible.sync="addCategoriesDialogVisible"
        width="28%"
        @close="colseDialog('addCategoriesRef')"
      >
        <!--    添加商品分类表单      -->
        <el-form :model="addCategoriesForm" :rules="addCategoriesFormRules" ref="addCategoriesRef" label-width="100px"
                 class="demo-ruleForm">
          <el-form-item label="分类名称：" prop="cat_name">
            <el-input v-model="addCategoriesForm.cat_name"></el-input>
          </el-form-item>

          <el-form-item label="父级分类：">
            <el-cascader
              v-model="categoriesData"
              :options="twoCategoriesList"
              :props="categoriesProps"
              :clearable="true"
              :blur="categoriesHandleChange"
              @change="categoriesHandleChange"></el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="addCategoriesDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addCategories">确 定</el-button>
          </span>
      </el-dialog>
      <tree-table
        ref="table"
        sum-text="sum"
        index-text="#"
        :data="categoriesList"
        :columns="columns"
        :stripe="props.stripe"
        :border="props.border"
        :show-header="props.showHeader"
        :show-summary="props.showSummary"
        :show-row-hover="props.showRowHover"
        :show-index="props.showIndex"
        :tree-type="props.treeType"
        :is-fold="props.isFold"
        :expand-type="props.expandType"
        :selection-type="props.selectionType">
        <template slot="isok" scope="scope">
          <i :class="scope.row.cat_deleted ? 'el-icon-error' : 'el-icon-success'"
             :style="scope.row.cat_deleted ? 'color: #F56C6C' : 'color: #67C23A'"></i>
          <!--              <i class="el-icon-error" style="color: #67C23A"></i>-->
        </template>
        <template slot="level" scope="scope">
          <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag v-else-if="scope.row.cat_level === 1" type="success">二级</el-tag>
          <el-tag v-else type="warning">三级</el-tag>
        </template>
        <template slot="operation" scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="">编辑</el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="deleteCategories(scope.row.cat_id)">
            删除
          </el-button>
          <el-dialog
            title="添加分类"
            :visible.sync="updateCategoriesDialogVisible"
            width="28%"
            @close="colseDialog('addCategoriesRef')"
          >
            <!--    添加商品分类表单      -->
            <el-form :model="updateCategoriesForm" :rules="updateCategoriesFormRules" ref="updateCategoriesRef"
                     label-width="100px"
                     class="demo-ruleForm">
              <el-form-item label="分类名称：" prop="cat_name">
                <el-input v-model="updateCategoriesForm.cat_name"></el-input>
            </el-form>
            <span slot="footer" class="dialog-footer">
            <el-button @click="addCategoriesDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addCategories">确 定</el-button>
          </span>
          </el-dialog>
        </template>
      </tree-table>
      <!--   分页区域   -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      categoriesList: [],
      twoCategoriesList: [],
      categoriesData: [],
      props: {
        stripe: false,
        border: true,
        showHeader: true,
        showSummary: false,
        showRowHover: true,
        showIndex: true,
        treeType: true,
        isFold: true,
        expandType: false,
        selectionType: false
      },
      addCategoriesDialogVisible: false,
      addCategoriesForm: {
        cat_pid: 0,
        cat_name: '',
        cat_level: 0
      },
      categoriesProps: {
        label: 'cat_name',
        value: 'cat_id',
        children: 'children',
        expandTrigger: 'hover',
        checkStrictly: true
      },
      addCategoriesFormRules: {
        cat_name: [
          {
            required: true,
            message: '请输入分类名称',
            trigger: 'blur'
          }, {
            min: 1,
            max: 10,
            message: '长度在 1 到 10 个字符',
            trigger: 'blur'
          }
        ]
      },
      // 总条目数
      total: 0,
      queryInfo: {
        type: 3,
        // 当前页码值
        pagenum: 1,
        // 每页显示多少条数据
        pagesize: 5
      },
      columns: [{
        label: '分类名称',
        prop: 'cat_name'
      }, {
        label: '是否有效',
        // 将当前列定义为模板列
        type: 'template',
        // 表示当前这一列使用的模板名称
        template: 'isok'
      }, {
        label: '排序',
        prop: 'cat_level',
        type: 'template',
        // 表示当前这一列使用的模板名称
        template: 'level'
      }, {
        label: '操作',
        type: 'template',
        // 表示当前这一列使用的模板名称
        template: 'operation'
      }]
    }
  },
  created () {
    this.getCategoriesList()
    console.log(this.categoriesList)
  },
  methods: {
    // 获取分类列表
    async getCategoriesList () {
      const { data: res } = await this.$http.get('categories', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // 总条目数
      this.total = res.data.total
      this.queryInfo.pagenum = res.data.pagenum + 1
      this.queryInfo.pagesize = res.data.pagesize
      this.categoriesList = res.data.result
    },
    // 获取二级分类列表
    async addCategoriesDialog () {
      const { data: res } = await this.$http.get('categories', {
        params: {
          type: 2
        }
      })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.twoCategoriesList = res.data
      this.addCategoriesDialogVisible = true
    },
    // 改变的每页显示条数
    handleSizeChange (pagesize) {
      this.queryInfo.pagesize = pagesize
      this.getCategoriesList()
    },
    // 改变的页数
    handleCurrentChange (pagenum) {
      console.log(pagenum)
      this.queryInfo.pagenum = pagenum
      this.getCategoriesList()
    },
    // 删除分类
    async deleteCategories (categoriesId) {
      const { data: res } = await this.$http.delete('categories/' + categoriesId)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.$message.success(res.meta.msg)
      this.getCategoriesList()
    },
    // 增加分类
    addCategories () {
      this.$refs.addCategoriesRef.validate(async vail => {
        if (!vail) return false
        if (this.addCategoriesForm.cat_level === '' || this.addCategoriesForm.cat_pid === undefined) {
          this.addCategoriesForm.cat_level = 0
          this.addCategoriesForm.cat_pid = 0
        }
        const { data: res } = await this.$http.post('categories', this.addCategoriesForm)
        if (res.meta.status !== 201) return this.$message.error(res.meta.msg)
        this.$message.success(res.meta.msg)
        this.addCategoriesDialogVisible = false
      })
    },
    // 获取级联获取到的数组
    categoriesHandleChange () {
      this.addCategoriesForm.cat_pid = this.categoriesData[this.categoriesData.length - 1]
      this.addCategoriesForm.cat_level = this.categoriesData.length
      console.log(this.addCategoriesForm)
    },
    // 重置表单
    colseDialog (refName) {
      this.$refs[refName].resetFields()
      this.categoriesData = []
      if (refName === 'addCategoriesRef') {
        this.addCategoriesForm.cat_pid = 0
        this.addCategoriesForm.cat_level = 0
        this.addCategoriesForm.cat_name = ''
      }
    }
  }
}
</script>
<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
